package com.example.qincetest.ui.activity

import android.os.Bundle
import android.webkit.WebResourceRequest
import android.webkit.WebView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewClientCompat
import androidx.webkit.WebViewFeature
import timber.log.Timber

class WebKit2Activity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    WebKit2Screen()
                }
            }
        }
    }
}

@Composable
fun WebKit2Screen() {
    var webViewVersion by remember { mutableStateOf("") }
    val context = LocalContext.current

    LaunchedEffect(Unit) {
        try {
            // 获取 WebView 版本号
            val packageInfo = context.packageManager.getPackageInfo("com.google.android.webview", 0)
            webViewVersion = "WebView 版本: ${packageInfo.versionName}"
            Timber.d("WebView version: ${packageInfo.versionName}")
        } catch (e: Exception) {
            webViewVersion = "无法获取 WebView 版本"
            Timber.e(e, "获取 WebView 版本失败")
        }
    }

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        Text(
            text = webViewVersion,
            modifier = Modifier.padding(16.dp),
            style = MaterialTheme.typography.bodyMedium
        )

        AndroidView(
            modifier = Modifier.weight(1f),
            factory = { context ->
                WebView(context).apply {
                    webViewClient = object : WebViewClientCompat() {
                        override fun shouldOverrideUrlLoading(
                            view: WebView,
                            request: WebResourceRequest
                        ): Boolean {
                            view.loadUrl(request.url.toString())
                            return true
                        }
                    }
                    
                    settings.apply {
                        javaScriptEnabled = true
                        domStorageEnabled = true
                        
                        // 使用 AndroidX WebKit 的特性
                        if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
                            WebSettingsCompat.setForceDark(
                                settings,
                                WebSettingsCompat.FORCE_DARK_AUTO
                            )
                        }
                        
                        if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
                            WebSettingsCompat.setForceDarkStrategy(
                                settings,
                                WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
                            )
                        }
                    }
                    
                    loadUrl("https://liulanmi.com/labs/core.html")
                }
            }
        )
    }
} 